package com.swingson.componentprocessor.impl;

import com.swingson.SwingsonGui;
import com.swingson.componentprocessor.BaseProcessor;
import com.swingson.exceptions.SwingsonException;
import com.swingson.jsonparser.JsonObject;
import javax.swing.JComponent;

public class CustomComponentProcessor extends BaseProcessor {
    private Class clazz;

    public CustomComponentProcessor(Class clazz) {
        this.clazz = clazz;
    }

    @Override
    public JComponent createComponent(JsonObject jsonElement, JComponent parent, SwingsonGui gui) {
        try {
            return (JComponent) clazz.newInstance();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new SwingsonException("Cannot instantiate class " + clazz.getSimpleName() + " for " + jsonElement.getAsString());
        }
    }

    @Override
    public Class[] getProcessingClass() {
        return new Class[]{clazz};
    }
}
